package com.example.messages.consumer;

import com.alibaba.fastjson.JSONObject;
import com.example.messages.entity.AppointmentInfo;
import com.example.messages.utils.SendMessage;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;


@Component
@RabbitListener(queues = "MessageService")
public class RabbitMQConsumer {

    @RabbitHandler
    public void process(@Header(name = "HealthBox") String type, @Payload byte[] message) {
        String mes = new String(message, StandardCharsets.UTF_8);
        System.out.println("收到的消息为： "+ mes);
        System.out.println("------------消息队列收到 【" + type + "】 消息 ------------ " );
        if (type.equals("RESULT")){
            System.out.println(SendMessage.sendResultMessage(mes));
        }

        if (type.equals("APPOINTMENT")){
            AppointmentInfo appointmentInfo = JSONObject.parseObject(mes,AppointmentInfo.class);
            System.out.println(SendMessage.sendAppointmentMessage(appointmentInfo));
        }
    }
}
